package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    TeacherDao TeacherDao = new TeacherDao();

    //把老师对象传达给Dao的办法，含boolean返回值，true代表添加成功
    public boolean addTeacher(Teacher t) {
        //1.接受老师对象，调用库管的addTeacher方法
        boolean flag = TeacherDao.addTeacher(t);
        //2.接受返回值，传给Controller
        return flag;
    }

    //接受Dao数组并遍历判断id是否重号的方法，含boolean返回值，true代表数组中有相同id
    public boolean isExists(String id) {
        //调用库管的查看数组方法
        Teacher[] T = TeacherDao.findAllStudent();
        //遍历数组判断id是否占用
        boolean flag = false;
        for (int i = 0; i < T.length; i++) {
            Teacher Temp = T[i];
            if (Temp != null && Temp.getId().equals(id)) {
                flag = true;
                break;
            }
        }
        //返回Boolean值给Controller
        return flag;
    }

    //调用Dao返还的数组，并判断里面是否控制null，有返还数组，没返还null的方法
    public Teacher[] findAllTeacher() {
        //向Dao拿数组，并返回给Controller
        Teacher[] T = TeacherDao.findAllStudent();
        //判断数组中是否有数据，有返回数组地址，没有返回null
        boolean flag = false;
        for (int i = 0; i < T.length; i++) {
            Teacher Temp = T[i];
            if (Temp != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            return T;
        } else {
            return null;
        }
    }

    //调用Dao的删除方法，含id参数的方法
    public void deleteTeacherById(String id) {
        TeacherDao.deleteTeacherById(id);
    }

    public void updateTeacher(String id, Teacher newTeacher) {
        //调用Dao的updateTeacher方法，把id和新对象传过去
        TeacherDao.updateTeacher(id, newTeacher);
    }
}
